<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      :visible.sync="visible">
      <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading"
               :class="method==='view'?'readonly':''" :disabled="method==='view'"
               label-width="120px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="姓名" prop="patientName"
                          :rules="[{required: true, message: '姓名不能为空', trigger: 'blur'}]">
              <el-input v-model="inputForm.patientName" placeholder="请填写姓名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idCardNumber"
                          :rules="[
                  {validator: validator.isCardId, trigger:'blur',required:true}
                 ]">
              <el-input v-model="inputForm.idCardNumber" placeholder="请填写身份证号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender"
                          :rules="[{required: true, message: '性别不能为空', trigger: 'blur'}]">
              <el-select v-model="inputForm.gender" placeholder="请选择" style="width: 100%;">
                <el-option
                  v-for="item in $dictUtils.getDictList('sex')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年龄" prop="age"
                          :rules="[
                  {validator: validator.isIntGteZero, trigger:'blur'}
                 ]">
              <el-input-number v-model="inputForm.age" placeholder="请填写年龄" style="width: 100%;"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="patientPhone"
                          :rules="[
                  {validator: validator.isMobile, trigger:'blur',required:true}
                 ]">
              <el-input v-model="inputForm.patientPhone" placeholder="请填写手机号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birthDate"
                          :rules="[
                 ]">
              <el-date-picker
                style="width: 100%;"
                v-model="inputForm.birthDate"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="住址" prop="address"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.address" placeholder="请填写住址"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="药物过敏史" prop="remarks">
              <el-select @change="drugChange" multiple v-model="selectDrug" placeholder="请选择" style="width: 100%;">
                <el-option
                  v-for="item in drugList"
                  :key="item.id"
                  :label="item.drugName"
                  :value="item.drugName">
                </el-option>
              </el-select>

            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method !== 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import patientInfoService from '@/api/patient/patientInfoService'
import userService from '@/api/sys/userService'
import drugInfoService from '@/api/drug/drugInfoService'

export default {
  data () {
    return {
      selectDrug: [],
      drugList: [],
      title: '',
      method: '',
      visible: false,
      loading: false,
      inputForm: {
        id: '',
        patientName: '',
        idCardNumber: '',
        gender: '',
        age: '',
        patientPhone: '',
        birthDate: '',
        address: '',
        guomin: ''
      },
      userForm: {
        id: '',
        companyDTO: { // 归属医院
          id: '',
          name: ''
        },
        officeDTO: {
          id: '',
          name: ''
        },
        roleIdList: [],
        postIdList: [],
        loginName: '', // 登录名
        no: '', // 工号
        name: '', // 姓名
        email: '', // 邮箱
        phone: '', // 电话
        mobile: '', // 手机
        loginFlag: '1', // 是否允许登陆
        photo: '', // 头像
        qrCode: '', // 二维码
        oldLoginName: '', // 原登录名
        newPassword: '', // 新密码
        confirmNewPassword: '',
        sign: '', // 签名
        remarks: '', // 备注
        idCardNumber: '',
        gender: '',
        age: '',
        birthDate: '',
        address: ''
      }
    }
  },
  components: {},
  created () {
    this.listDrug()
  },
  methods: {
    init (method, id) {
      this.method = method
      this.inputForm.id = id
      if (method === 'add') {
        this.title = `新建病人信息`
      } else if (method === 'edit') {
        this.title = '修改病人信息'
      } else if (method === 'view') {
        this.title = '查看病人信息'
      }
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          patientInfoService.queryById(this.inputForm.id).then(({data}) => {
            this.inputForm = this.recover(this.inputForm, data)
            this.loading = false
          })
        }
      })
    },

    drugChange (val) {
      this.inputForm.guomin = val.join(',')
    },

    listDrug () {
      drugInfoService.listAll(this.drugSearchForm).then(res => {
        this.drugList = res.data
      })
    },

    // 表单提交
    doSubmit () {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          console.log('idCardNumber', this.inputForm.idCardNumber)
          patientInfoService.save(this.inputForm).then(({data}) => {
            this.visible = false
            this.$message.success('操作成功')
            this.$emit('refreshDataList')
            this.loading = false
            if (this.method === 'add') {
              this.saveUser(data.id)
            }
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    // 保存用户
    saveUser (id) {
      // 赋值操作
      this.userForm.id = id
      this.userForm.name = this.inputForm.patientName
      this.userForm.idCardNumber = this.inputForm.idCardNumber
      this.userForm.gender = this.inputForm.gender
      this.userForm.age = this.inputForm.age
      this.userForm.mobile = this.inputForm.patientPhone
      this.userForm.birthDate = this.inputForm.birthDate
      this.userForm.address = this.inputForm.address
      this.userForm.roleIdList = ['17bac1f980264e3e8193bc965538e2c6']
      this.userForm.loginName = this.inputForm.idCardNumber
      this.userForm.companyDTO = {id: '1504779078102695938', name: '某三甲医院'}
      this.userForm.officeDTO =
        {id: '1765992465909907458', name: '患者部门'}
      this.userForm.no = 1
      this.userForm.newPassword = '123456'
      this.userForm.confirmNewPassword = '123456'
      userService.saveUser(this.userForm)
    }
  }
}
</script>


